package com.atwisdom.star.core.dao.pojo.modelClass.hier;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.util.JsonUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class HierInstanceUtils {
    private Long id;
    private String type;
    private String className;
    private Long containerId;
    private String containerModel;
    private List<HierInstance> hierInstances;
    private List<HierModel> models;
    private HierPropertiesUtils hierPropertiesUtils;

    public HierInstanceUtils(List<HierInstance> hierInstances,List<HierModel> models) {
        if (hierInstances != null && hierInstances.size() > 0) {
            HierInstance hierInstance = hierInstances.get(0);
            this.type = hierInstance.getType();
            this.className = hierInstance.getClassName();
            this.containerId = hierInstance.getContainerId();
            this.containerModel = hierInstance.getContainerModel();
            this.hierInstances = hierInstances;
            this.models = models;
            this.hierPropertiesUtils = new HierPropertiesUtils(models);
        }
    }

    public JSONObject fetchHier(){
        if (hierInstances == null || hierInstances.size() <1) {
            return new JSONObject();
        }

        if (models == null || models.size() <1) {
            return new JSONObject();
        }

        JSONObject hier = new JSONObject();
        hier.put("type",this.type);
        hier.put("className",className);
        hier.put("containerId",containerId);
        hier.put("containerModel",containerModel);
        List<JSONObject> contentList = new ArrayList<>();
        this.hierInstances.forEach(ins->{
            //构造link
            JSONObject link = new JSONObject();
            link.put("linkId", ins.getLinkId());
            link.put("linkModel", ins.getLinkModel());
            link.put("linkClassName", ins.getLinkClassName());
            link.put("linkType", ins.getLinkType());
            JSONObject content = new JSONObject();
            content.put("id",ins.getId());
            content.put("name", ins.getName());
            content.put("parentId",ins.getParentId());
            content.put("prev",ins.getPrev());
            content.put("nodeId", ins.getNodeId());
            content.put("nodeType", ins.getNodeType());
            content.put("values", ins.getValues());
            this.buildHierStatus(content,ins);
            content.put("link", link);
            contentList.add(content);
        });
        hier.put("content",contentList);
        return hier;
    }
    private void buildHierStatus(JSONObject content,HierInstance instance){
        HierModel hierModel;
        if("list".equalsIgnoreCase(this.type)){
            hierModel = this.models.get(0);
        }else{
            hierModel = this.models.stream().filter(m ->m.getNodeId() != null && m.getNodeId().equalsIgnoreCase(instance.getNodeId())).findFirst().orElse(null);
        }
        setInsStatus(hierModel,content, instance);
    }

    private void setInsStatus(HierModel hierModel,JSONObject content, HierInstance instance) {
        if (hierModel != null) {
            JSONArray statusProperties = hierModel.getStatusProperties();
            JSONObject insJsonObject = JSONObject.parseObject(JSON.toJSONString(instance));
            if (statusProperties != null) {
                for (Object item : statusProperties) {
                    JSONObject itemJson = JsonUtil.toJSONObject(item);
                    String code = itemJson.getString("code");
                    String name = itemJson.getString("name");
                    Object o = insJsonObject.get(code);
                    if(o==null){
                        o=itemJson.get("(defaultValue");
                    }
                    content.put(name,o);
                }
            }

        }
    }
}
